package com.adingxiong.quickstart.project.controller;


import com.adingxiong.quickstart.project.entity.GoodsContent;
import com.adingxiong.quickstart.project.entity.GoodsInfo;
import com.adingxiong.quickstart.project.entity.Kpi;
import com.adingxiong.quickstart.project.entity.po.GoodsInfoPo;
import com.adingxiong.quickstart.project.entity.vo.GoodsDetailVo;
import com.adingxiong.quickstart.project.entity.vo.GoodsInfoVo;
import com.adingxiong.quickstart.project.service.IGoodsContentService;
import com.adingxiong.quickstart.project.service.IGoodsInfoService;
import com.adingxiong.quickstart.project.service.IKpiService;
import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.adingxiong.quickstart.util.RandomUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * kpi属性id 前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/goods-info")
@Api(tags = "商品管理")
public class GoodsInfoController {

    @Autowired
    private IGoodsInfoService goodsInfoService;

    @Autowired
    private IKpiService kpiService;

    @Autowired
    private IGoodsContentService contentService;


    @ApiOperation(value = "获取首页基本列表信息")
    @GetMapping("/homeInfo")
    public Result<Page<GoodsInfoVo>> getHomeMentions(
            @ApiParam(value = "商品名称") @RequestParam(value = "goodsName",required = false) String goodsName,
            @ApiParam(value = "商品标题") @RequestParam(value = "title" ,required = false) String title,
            @ApiParam(value = "当前页数") @RequestParam(value = "currentPage" ,required = false,defaultValue = "0")Integer currentPage,
            @ApiParam(value = "每页条数") @RequestParam(value = "pageSize" ,required = false,defaultValue = "10")Integer pageSize
    ){
        Map<String,Object> param = new HashMap<>();
        if(!StringUtils.isEmpty(goodsName)){
            param.put("goodsName",goodsName);
        }
        if(!StringUtils.isEmpty(title)){
            param.put("title",title);
        }

        param.put("currentPage",currentPage);
        param.put("pageSize",pageSize);

        List<GoodsInfoVo> list = goodsInfoService.findHomeContent(param);
        Page<GoodsInfoVo> page = new Page<>();
        if(list != null && list.size()>0){
            page.setRecords(list);
            page.setTotal(list.size());
            page.setCurrent(currentPage);
            page.setPages(pageSize);
        }
        return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),page);
    }

    @ApiOperation(value = "商品信息详情查询")
    @GetMapping(value = "/goodsInfo")
    public Result<Page<GoodsDetailVo>> getGoodsInfo(
            @ApiParam(value = "商品名称") @RequestParam(value = "goodsName",required = false) String goodsName,
            @ApiParam(value = "商品标题") @RequestParam(value = "title" ,required = false) String title,
            @ApiParam(value = "当前页数") @RequestParam(value = "currentPage" ,required = false,defaultValue = "0")Integer currentPage,
            @ApiParam(value = "每页条数") @RequestParam(value = "pageSize" ,required = false,defaultValue = "10")Integer pageSize
    ){
        QueryWrapper<GoodsInfo> query = new QueryWrapper<>();
        if(!StringUtils.isEmpty(goodsName)){
            query.like("goods_name",goodsName);
        }
        if(!StringUtils.isEmpty(title)){
            query.like("title",title);
        }
        Page page = new Page();
        page.setCurrent(currentPage);
        page.setPages(pageSize);

        Page<GoodsInfo> res = goodsInfoService.page(page,query);
        List<GoodsDetailVo> voList = new ArrayList<>();

        Page<GoodsDetailVo> result = new Page<>();
        if(res != null){
            List<GoodsInfo> info = res.getRecords();
            for(GoodsInfo goodsInfo:info){
                //根据goods_id查询相关联的属性信息
                GoodsDetailVo detailVo = new GoodsDetailVo(goodsInfo);
                List<GoodsContent> contents = contentService.listByGoodsId(goodsInfo.getId());
                detailVo.setGoodsContent(contents);
                detailVo.setKpi(kpiService.getKpiByGoodsId(goodsInfo.getId()));
                voList.add(detailVo);
            }
            result.setRecords(voList);
            result.setTotal(res.getTotal());
            result.setCurrent(currentPage);
            result.setSize(pageSize);

        }
        return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),result);
    }


    @ApiOperation(value = "保存(修改)商品的所有信息")
    @PostMapping("/save")
    @Transactional
    public Result saveAll(@RequestBody GoodsInfoPo goodsInfoPo){
        if(goodsInfoPo  == null) {
            return new Result(RetCode.PARAM_ERROR.getCode(), RetCode.PARAM_ERROR.getMsg(), false);
        }
        int id = RandomUID.getUUID10();
        goodsInfoPo.setId(id);
        Kpi kpi = goodsInfoPo.getKpi();
        List<GoodsContent> contents = goodsInfoPo.getGoodsContents();
        boolean parent =false;
        try{
            goodsInfoPo.setKpi(null);
            parent = goodsInfoService.saveOrUpdate(goodsInfoPo);
            if(kpi != null){
                kpi.setGoodsId(id);
                kpiService.saveOrUpdate(kpi);
            }
            if(contents !=null && contents.size()>0){
                for(GoodsContent vo :contents){
                    vo.setGoodsId(id);
                }
                contentService.saveOrUpdateBatch(contents);
            }
            return new Result(parent ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(),parent ? "操作成功" : "操作失败",parent);
        } catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new Result(RetCode.SYSTEM_ERROR.getCode(),e.getMessage(),e);
        }
    }


    @ApiOperation(value = "根据id删除商品信息")
    @GetMapping("/del")
    @Transactional
    public Result del (@ApiParam(value = "唯一标识",required = true) @RequestParam(value = "id") Integer id){
        if(id ==null){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),false);
        }
        try {
            GoodsInfo goodsInfo = goodsInfoService.getById(id);
            if (goodsInfo != null) {
                boolean del = goodsInfoService.removeById(id);

                Kpi kpiIds = kpiService.getKpiByGoodsId(id);
                if (!StringUtils.isEmpty(kpiIds)) {
                    kpiService.removeById(kpiIds.getId());
                }
                return new Result(del ? RetCode.SUCCESS.getCode() : RetCode.SYSTEM_ERROR.getCode(), del ? "删除成功" : "删除失败", del);
            }
            return new Result(RetCode.PARAM_VALUE_ERROR.getCode(), "当前数据不存在", false);
        } catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new Result(RetCode.SYSTEM_ERROR.getCode(),e.getMessage(),e);
        }
    }

}

